import time
from . import DBUtils
# import DBUtils


class SqlOperation:

    @staticmethod
    def insertUserData(params):
        if params[0] == 'lisi3':
            sql1 = "select * from user where username = %s and password = md5(%s)"
            data = DBUtils.select(sql1,params)
            # 不存在，进行插入操作
            if len(data) > 0:
                pass
            else:
                sql2 = "insert into user(username,password) values(%s,md5(%s))"
                DBUtils.update(sql2, params)
            time.sleep(1)

    @staticmethod
    def deleteUserData(params):
        if params[0] == 'lisi3':
            time.sleep(1)
            sql1 = 'delete from user where username = %s and password = md5(%s)'
            DBUtils.update(sql1,params)


    @staticmethod
    def selectUserByName(params):
        sql = 'select * from user where username = %s'
        return DBUtils.select(sql,params)


    @staticmethod
    def deleteUserByName(params):
        time.sleep(1)
        sql1 = 'delete from user where username = %s'
        DBUtils.update(sql1, params)

    @staticmethod
    def addUser(params):
        sql = 'insert into user(username,password) values(%s,md5(%s))'
        DBUtils.update(sql,params)
        time.sleep(1)

    @staticmethod
    def selectByBankNameAndNumber(params):
        sql = 'select * from bankcard where cardBank = %s and cardNum = %s'
        return DBUtils.select(sql, params)

    @staticmethod
    def delBankCardByNameAndNumber(params):
        time.sleep(1)
        sql = 'delete from bankcard where cardBank = %s and cardNum = %s'
        DBUtils.update(sql, params)

    @staticmethod
    def selectByBankNumber(params):
        sql = "select * from bankcard where cardNum = %s"
        return DBUtils.select(sql,params)

    @staticmethod
    def addBankCard(params):
        sql = 'insert into bankcard values(null,%s,%s,%s,%s,%s,%s)'
        DBUtils.update(sql, params)

    @staticmethod
    def delBankCardByBankNumber(params):
        time.sleep(1)
        sql = 'delete from bankcard where cardNum = %s'
        DBUtils.update(sql,params)

    @staticmethod
    def selectBankByUserIdAndDefult(params):
        time.sleep(1)
        sql = 'select * from bankcard where userId = %s and defaultl = 1'
        return DBUtils.select(sql,params)

    # 查询借贷条数 根据用户 的id
    @staticmethod
    def selectLoansByUserId(params):
        sql = 'select count(*) from loan where userId = %s'
        return DBUtils.select(sql,params)



